/**
 * Created by LINLA5 on 8/25/2016.
 */
angular.module('order').controller('waitAndRecieveOrderController', function ($scope, $http, $routeParams, $rootScope, $interval) {
    var pendingOrder = [];
    $http.get('/order/customer'+ "?action=1").then(function (res) {
        var data = res.data;
        if (data.success == true) {
            $scope.waitingForReceivingOrders = data.data;
            //$scope.status = 1;

        }
    }, function (res) {
        console.error(res.data.msg);
    });

    $interval(function () {
        if($scope.waitingForReceivingOrders){
            $scope.waitingForReceivingOrders.forEach(function (order) {
                if(order.status == 0){
                    console.log('1111');
                    $http.get('/order/refresh/'+order._id)
                        .then(function (res) {
                            var data = res.data;
                            order.status = data.order.status;
                        }, function (res) {

                        })
                }
            })
        }
    },2000);

    $scope.confirmOrder = function (order, index) {
        $http.put("/order/" + order._id + "?action=1").then(function (res) {
            var data = res.data;
            if (data.success = true) {
                $scope.waitingForReceivingOrders.splice(index, 1);
                window.location = "#/order/history"
            }
        }, function (res) {
            console.erro(res.data.msg);
        })
    };

});

angular.module('order').filter('orderStatusFilter', function () {
    return function(status){
        if (status == 0) {
            return "商家未接单";
        } else if (status == 1) {
            return "商家已接单";
        } else if (status == 2) {
            return "商家已拒单";
        }
    }
});